<?php

class DefaultController extends Controller
{
	public function actionIndex()
	{
		$model=new Members('search');
		$model->unsetAttributes();
		if(isset($_GET['Members']))
			$model->attributes=$_GET['Members'];

		$this->render('index', array(
			'model'=>$model
		));
	}

	public function actionView($id)
	{
		$model = $this->loadModel($id);

		$this->render('view',array(
			'model'=>$model,
		));
	}

	public function actionCreate()
	{
		$model=new Members('create');

		if(isset($_POST['Members']))
		{
			$model->attributes=$_POST['Members'];
			$model->dateofbirth = date('Y-m-d', strtotime($model->dateofbirth));
			$model->altphone = '';
			$model->city = '';
			$model->country = '';
			$model->totalpoints = 0;
			$model->newsletter = 'Y';
			$model->type = 'E';
			$model->lastlogin = $model->createddate = date('Y-m-d H:i:s');
			$model->oldsalt = '';
			$model->oldpassword = '';

			// Save Members image
			$imagePath = IMAGES_DIR . 'Avatar/' . date('Ymd', strtotime($model->createddate)) . '/';
			$imageUploadFile	= CUploadedFile::getInstance($model, 'image');
			if ($imageUploadFile !== null) {
				$model->image = $model->username . '.' . $imageUploadFile->extensionName;
				if (!file_exists($imagePath)) {
					mkdir($imagePath, 0777, true);
				}
				$imageUploadFile->saveAs($imagePath . $model->image);
				// Resize images
//				$this->resizeMembersImage($imagePath, $model->image, date('Y-m-d H:i:s'));
			}
			else {
				$model->image = 'noavatar.jpg';
			}
			// Save Members
			if ($model->validate()) {
				$model->password = CPasswordHelper::hashPassword($model->password);
				if($model->save())
					$this->redirect(array('view', 'id'=>$model->id));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Members']))
		{
			$model->attributes=$_POST['Members'];

			// Save Members image
			$imagePath = IMAGES_DIR . 'Avatar/' . date('Ymd', strtotime($model->createddate)) . '/';
			$imageUploadFile	= CUploadedFile::getInstance($model, 'image');
			if ($imageUploadFile !== null) {
				$model->image = $model->username . '.' . $imageUploadFile->extensionName;
				// Dont need to create folder because it's always exist
				if (!file_exists($imagePath)) {
					mkdir($imagePath, 0777, true);
				}
				$imageUploadFile->saveAs($imagePath . $model->image);
				// Resize images
				//$this->resizeMembersImage($imagePath, $model->image, date('Y-m-d H:i:s'));
			}
			if($model->save()) {
				$this->redirect(array('view', 'id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	public function loadModel($id)
	{
		$model=Members::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	public function actionActive($id)
	{
		$model =Members::model()->findByPk($id);

		if (!empty($model)) {
			$model->updateByPk($id, array('status'=>'A'));
		}

		$this->redirect(array('Users/default/index'));
	}

	public function actionBanned($id)
	{
		$model =Members::model()->findByPk($id);

		if (!empty($model)) {
			$model->updateByPk($id, array('status'=>'I'));
		}

		$this->redirect(array('Users/default/index'));
	}
}